 ########################                     
##     COPYRIGHT(C):    ##
# TOMMY ANDRÉ BERTELSEN  #
#        480266          #
# NEURAL NETWORK TRANIER #
## 'NN-trainer pybrain' ##
 ########################

#Libraries/modules:
import pybrain
import pickle

#Modules/libraries:
from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure import TanhLayer
from pybrain.structure import SoftmaxLayer



# Some sizes:
inputNodes = 15
midNodes = 20
outputNode = 1

#init dataset:
ds = SupervisedDataSet(inputNodes, outputNode)

# Open file with data:
f = open('pptotal.txt')
lines = f.readlines()
f.close()

# fill up lines (list):
lines = [line.strip() for line in open('pptotal.txt')]

s = 1000.0

# Add samples from file:
for i in range(0, len(lines)-16):
	ds.addSample((float(lines[i])/s, float(lines[i+1])/s, float(lines[i+2])/s, float(lines[i+3])/s, float(lines[i+4])/s, float(lines[i+5])/s, float(lines[i+6])/s, float(lines[i+7])/s, float(lines[i+8])/s, float(lines[i+9])/s, float(lines[i+10])/s, float(lines[i+11])/s, float(lines[i+12])/s, float(lines[i+13])/s, float(lines[i+14])/s),(float(lines[i+15])/s))
# Build network and trainer
net = buildNetwork(inputNodes, midNodes, midNodes, outputNode)

trainer = BackpropTrainer(net, ds, learningrate=0.1, momentum=0.15, verbose=True)

#Train until error converges:
net.reset()
trainer.trainUntilConvergence(maxEpochs=3000)
#print trainer.train()


#Trainer loop here:
#for i in range(0, 10000):
#	print trainer.train()

#trainer.trainOnDataset(ds, 1000)

#print net.activate([100.30, 125.47, 200.45, 400.36, 232.33, 123.21, 890.36]);
#print net.activate([355.43, 349.37, 443.62, 410.86, 397.14, 710.87, 771.10]);

# Testset:
p = 0
totalPerCent = 0.0
for i in range(0, len(lines) - 15):
	estimated = net.activate([float(lines[i])/s, float(lines[i+1])/s, float(lines[i+2])/s, float(lines[i+3])/s, float(lines[i+4])/s, float(lines[i+5])/s, float(lines[i+6])/s, float(lines[i+7])/s, float(lines[i+8])/s, float(lines[i+9])/s, float(lines[i+10])/s, float(lines[i+11])/s, float(lines[i+12])/s, float(lines[i+13])/s, float(lines[i+14])/s ]) * 1000.0;
	print estimated
	estimatedPerCent = 100.0-((float(estimated)/float(lines[i+15]))*100.0)
	print estimatedPerCent
	totalPerCent += estimatedPerCent
	p += 1

totalPerCent = totalPerCent/float(p)
print "Total percent error: "
print totalPerCent


#Save the trained network:
fileObject = open('datFile.xml', 'w')
pickle.dump(net, fileObject)
fileObject.close()




# Prints nodes and parameters:
#for mod in net.modules:
 # print "Module:", mod.name
  #if mod.paramdim > 0:
   # print "--parameters:", mod.params
  #for conn in net.connections[mod]:
  #  print "-connection to", conn.outmod.name
   
# if conn.paramdim > 0:  
#    print "- parameters", conn.params
  #if hasattr(net, "recurrentConns"):
   # print "Recurrent connections"
    #for conn in net.recurrentConns:             
     #  print "-", conn.inmod.name, " to", conn.outmod.name
      # if conn.paramdim > 0:
       #   print "- parameters", conn.params
